from django.contrib.auth import logout
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def register(request):
    '''User Register to create a new account'''
    # logout first
    logout(request)

    if request.method == 'POST':
        ucform = UserCreationForm(request.POST)
        if ucform.is_valid():
            new_user = ucform.save()
            return HttpResponseRedirect('/accounts/login/?next=/')
    else:
        ucform = UserCreationForm()

    _c = {'form': ucform}
    _c.update(csrf(request))

    return render_to_response('accounts/register.html', _c)
